package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckgroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
public class CheckgroupServiceImpl implements CheckgroupService {
    @Autowired
    CheckgroupDao checkgroupDao;

    /*
    * 1.添加检查组对象,回显主键id
      2.维护中间表的数据(检查组和检查项的关系)
    * */
    @Transactional
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        checkgroupDao.add(checkGroup);

        if (checkGroup.getId() != null && checkItemIds != null && checkItemIds.length > 0) {
            for (Integer checkItemId : checkItemIds) {
                checkgroupDao.set(checkGroup.getId(), checkItemId);
            }
        } else {
            throw new RuntimeException("检查项和检查组中间表维护失败");
        }
    }

    /*
     * 1.开始分页
     * 2.根据条件查询(不用添加分页参数)
     * 3.返回PageResult对象
     * */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkgroupDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkgroupDao.findById(id);
    }

    @Override
    public Integer[] findCheckitemIdsByCheckGroupId(Integer id) {
        return checkgroupDao.findCheckitemIdsByCheckGroupId(id);
    }


    @Override
    @Transactional
    @RequestMapping("/edit")
    public void edit(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup) {
        //1.修改检查组
        checkgroupDao.edit(checkGroup);
        //2.先删除该检查组原来的关系
        checkgroupDao.deleteAssociation(checkGroup.getId());

        //2.2添加新的关系
        if (checkGroup.getId() != null && checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                checkgroupDao.set(checkGroup.getId(), checkitemId);
            }
        }
    }

    /*
     * 1.判断检查组是否被套餐关联,如果关联,不能删除,如果没有关联,继续第二步
     * 2.先删除检查组与检查项的关系,再删除检查组
     *
     * */
    @Override
    public void deleteById(Integer id) {
        Long count = checkgroupDao.findSetmealCountByCheckGroupId(id);
        if (count == 0) {
            checkgroupDao.deleteAssociation(id);
            checkgroupDao.deleteById(id);
        } else {
            throw new RuntimeException("检查组被套餐关联,不能删除!!!");
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkgroupDao.findAll();
    }
}
